package com.guoxianwei.scm.web.actions;

import java.io.StringReader;

import javax.servlet.http.HttpSession;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SessionScope;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;

import com.guoxianwei.scm.domain.Account;
import com.guoxianwei.scm.service.AccountService;

@SessionScope
public class AccountActionBean extends AbstractActionBean {

	private static final long serialVersionUID = 5499663666155758178L;

	private static final String NEW_ACCOUNT = "/WEB-INF/jsp/account/NewAccountForm.jsp";
	private static final String EDIT_ACCOUNT = "/WEB-INF/jsp/account/EditAccountForm.jsp";
	private static final String SIGNON = "/WEB-INF/jsp/HelloWord.jsp";
    @SpringBean
	private transient AccountService accountService;

	private Account account = new Account();

	private boolean authenticated;

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public String getUsername() {
		return account.getUsername();
	}

	@Validate(required = true, on = { "signon", "newAccount", "editAccount" })
	public void setUsername(String username) {
		account.setUsername(username);
	}

	public String getPassword() {
		return account.getPassword();
	}

	@Validate(required = true, on = { "signon", "newAccount", "editAccount" })
	public void setPassword(String password) {
		account.setPassword(password);
	}

	public Resolution newAccountForm() {
		return new ForwardResolution(NEW_ACCOUNT);
	}

	public Resolution newAccount() {
		
		accountService.insertAccount(account);
		account = accountService.getAccount(account.getUsername());
		authenticated = true;
		return new RedirectResolution(CatalogActionBean.class);
	}

	public Resolution editAccountForm() {
		return new ForwardResolution(EDIT_ACCOUNT);
	}

	public Resolution editAccount() {
		accountService.updateAccount(account);
		account = accountService.getAccount(account.getUsername());
		return new RedirectResolution(CatalogActionBean.class);
	}

	@DefaultHandler
	public Resolution signonForm() {
		return new ForwardResolution(SIGNON);
	}

	public Resolution signon() {

		account = accountService.getAccount(getUsername(), getPassword());

		if (account == null) {
			String value = "Invalid username or password.  Signon failed.";
			setMessage(value);
			clear();
			return new StreamingResolution("text", new StringReader("false"));
		} else {
			account.setPassword(null);
			authenticated = true;
			HttpSession s = context.getRequest().getSession();
			// this bean is already registered as /actions/Account.action
			s.setAttribute("accountBean", this);
			return new StreamingResolution("text", new StringReader("true"));
		}
	}

	public Resolution signoff() {
		context.getRequest().getSession().invalidate();
		clear();
		return new RedirectResolution(CatalogActionBean.class);
	}

	public boolean isAuthenticated() {
		return authenticated && account != null
				&& account.getUsername() != null;
	}

	public void clear() {
		account = new Account();
		authenticated = false;
	}

}
